package com.tenebrous.jestic.controller.framwork.system.params;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.SysParams;
import com.tenebrous.jestic.service.framwork.system.SysParamsService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/params")
public class ParamsController extends SupperController {

    @Autowired
    private SysParamsService userParamsService;

    /**
     * 查询对象
     *
     * @param id
     * @return
     */
    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        SysParams item = userParamsService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败。");
    }

    /**
     * 查询用户参数
     *
     * @param userid
     * @param skeys
     * @return
     */
    @PostMapping("/get-params")
    public ApiResult getParams(@NotBlank String userid, @NotEmpty String[] skeys) {

        List<Map<String, Object>> list = userParamsService.getParams(userid, skeys);
        if (!list.isEmpty()) {
            return new ApiResult(200, "查询成功。", list);
        }
        return new ApiResult(200, "查询成功。");
    }

    /**
     * 查询用户参数
     * @param skey
     * @param userid
     * @return
     */
    @PostMapping("/skey/get")
    public ApiResult getBySkey(@NotBlank String skey, String userid) {
        List<SysParams> list = userParamsService.queryParamsBySkey(skey, userid);
        if (list.size() > 0) {
            return new ApiResult(200, "查询成功。", list.get(0));
        } else {
            return new ApiResult(999, "查询失败，为找到指定记录。");
        }
    }

    /**
     * 新增/更新
     *
     * @param fitem
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新系统参数", method = WeblogEnum.UPDATE)
    public ApiResult updateParams(@NotBlank String fitem) {
        return userParamsService.updateParams(fitem);
    }

    /**
     * 新增/更新
     *
     * @param skey
     * @param svalue
     * @param sname
     * @return
     */
    @PostMapping("/skey/supdate")
    @Weblog(description = "更新系统参数", method = WeblogEnum.UPDATE)
    public ApiResult supdate(@NotBlank String skey, @NotBlank String svalue, String sname, String userid) {
        SysParams item = null;
        OrgUser ouser = super.getCurrentUser();
        List<SysParams> list = userParamsService.queryParamsBySkey(skey, userid);
        if (list.size() > 0) {
            item = list.get(0);
            item.setSvalue(svalue);
            item.setSname(StringUtils.isBlank(sname) ? null : sname);
            userParamsService.updateSelective(item);
        } else {
            item = new SysParams();
            item.setId(UUIDUtils.randomUUID());
            item.setUserid(userid);
            item.setSkey(skey);
            item.setSvalue(svalue);
            item.setSname(StringUtils.isBlank(sname) ? null : sname);
            item.setCreateDate(new Date());
            item.setCreateUserid(ouser.getId());
            item.setCreateUsername(ouser.getRealname());
            userParamsService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。", item);
    }
}
